package elicecn.util.mail;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * 发送邮件
 * 
 * @author 屹砾
 * @email eli.wuhan@gmail.com
 * @time 2009-11-25
 */
public class SendMail implements Serializable {
	private static final long serialVersionUID = -3844395161059933654L;
	protected static final Log log = LogFactory.getLog(SendMail.class);
	public static final String UTF_8 = "UTF-8";
	private HtmlEmail htmlEmail;

	/**
	 * 构造方法
	 * 
	 * @param host
	 *            smtp服务器
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @param from
	 *            发信邮箱
	 * @param to
	 *            目标邮箱
	 * @param subject
	 *            主题
	 * @throws EmailException
	 */
	public SendMail(String host, String username, String password, String from,
			String[] to, String subject) throws EmailException {
		htmlEmail = new HtmlEmail();
		htmlEmail.setHostName(host);
		htmlEmail.setAuthentication(username, password);
		htmlEmail.setFrom(from);
		for (String email : to) {
			htmlEmail.addTo(email);
		}
		htmlEmail.setSubject(subject);
	}

	/**
	 * 发送邮件(邮件内容从模板中提取)
	 * 
	 * @param templateParamMap
	 *            参数列表
	 * @param templateDir
	 *            模板目录
	 * @param templateFile
	 *            模板文件
	 * @throws IOException
	 * @throws TemplateException
	 * @throws EmailException
	 */
	@SuppressWarnings("unchecked")
	public void send(Map templateParamMap, String templateDir,
			String templateFile) throws IOException, TemplateException,
			EmailException {
		send(templateParamMap, templateDir, templateFile, UTF_8, UTF_8);
	}

	/**
	 * 发送邮件(邮件内容从模板中提取)
	 * 
	 * @param templateParamMap
	 *            参数列表
	 * @param templateDir
	 *            模板目录
	 * @param templateFile
	 *            模板文件
	 * @param templateEncoding
	 *            编码方式
	 * @param emailEncoding
	 *            编码方式
	 * @throws IOException
	 * @throws TemplateException
	 * @throws EmailException
	 */
	@SuppressWarnings("unchecked")
	public void send(Map templateParamMap, String templateDir,
			String templateFile, String templateEncoding, String emailEncoding)
			throws IOException, TemplateException, EmailException {
		Configuration cfg = new Configuration();
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		cfg.setDirectoryForTemplateLoading(new File(templateDir));
		cfg.setDefaultEncoding(templateEncoding);
		Template template = cfg.getTemplate(templateFile);
		StringWriter stringWriter = new StringWriter();
		template.process(templateParamMap, stringWriter);
		htmlEmail.setCharset(emailEncoding);
		htmlEmail.setHtmlMsg(stringWriter.toString());
		htmlEmail.send();
		// log.info(stringWriter.toString());
	}
}